package com.yihen.service.impl;

import com.yihen.dto.RegistrationDto;
import com.yihen.dto.Result;
import com.yihen.mappers.RegistrationMapper;
import com.yihen.pojo.Registration;
import com.yihen.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Constants;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationMapper registrationMapper;

    @Override
    public Result addRegistration(Registration registration) {
        // 添加挂号对象
        try {
            int tag = registrationMapper.insert(registration);
        } catch (Exception e) {
            // 添加失败，返回失败信息
            return new Result(Constants.ADDFAILED);
        }
        // 添加成功，返回成功信息
        return new Result(null, true, Constants.ADDSUCCESS);
    }

    @Override
    public Result getAllRegistrationDto() {
        List<RegistrationDto> registrationDtos = registrationMapper.queryAllRegistrationDto();
        return new Result(registrationDtos);
    }

    @Override
    public Result getRegistrationDtoByPatientId(long id) {
        List<RegistrationDto> registrationDtos = registrationMapper.queryRegistrationDtoByPatientId(id);
        return new Result(registrationDtos);
    }

    @Override
    public Result getRegistrationDtoByDoctorId(long id) {
        List<RegistrationDto> registrationDtos = registrationMapper.queryRegistrationDtoByDoctorId(id);
        return new Result(registrationDtos);

    }

    @Override
    public Result getRegistrationDtoById(long id) {
       RegistrationDto registrationDto = registrationMapper.queryRegistrationDtoById(id);
        return new Result(registrationDto);
    }

    @Override
    public Result getRegistrationDtoByCondition(RegistrationDto dto) {
        if (dto.getTime() != null) {
            SimpleDateFormat format = new SimpleDateFormat("Y-M-d");
            String timeFormat = format.format(dto.getTime());
            dto.setTimeFormat(timeFormat);
        }
        List<RegistrationDto> registrationDtos = registrationMapper.queryRegistrationDtoByCondition(dto);
        return new Result(registrationDtos);
    }
}
